        package com.yq.leetcodeOld;

import com.yq.leetcodeOld.tree.ListNode;

/**
 * @ClassName ReverseBetween
 * @Description TODO
 * @Author yaoqiang
 * @Date 2022/4/8 15:29
 * @Version 1.0
 */
public class ReverseBetween {
    public static void main(String[] args) {


    }
    public ListNode reverseBeteen(ListNode head, int left, int right){

        //输出：[1,4,3,2,5]
        //12345  2和4 ，从1432 5
        //思路， 双指针，
        if (head==null|| head.next==null){ return head;}
        ListNode pre=null,reverseList=null,cur=head;
        boolean isNeed=false;

        while (cur!=null){
            ListNode temp= cur.next;
            if (cur.val==left){ //遇到left
                isNeed=true;
                cur.next=reverseList;
                reverseList=cur;
                cur=temp;
            }
            if(!isNeed){
            //还未反转，正常赋值 不断将当前值给到pre
                if (pre==null){
                    pre=cur;
                }else {
                    pre.next=cur;
                }
                cur=temp;
            }else{
                //说明已经正在反转了,遇到停的标志就停
                if(cur.val==right){
                    //到结束的标志了
                    cur.next=reverseList;
                    reverseList=cur;
                    reverseList.next=temp;
                    pre.next=reverseList;
                    break;
                }else {
                    cur.next=reverseList;
                    reverseList=cur;
                    cur=temp;
                }
            }
        }
        return pre;
    }

}
